﻿using Ledict.ConditionExtract.Common;
using Ledict.ConditionExtract.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ledict.ConditionExtract.DAL
{
    /// <summary>
    /// 地域违法行为与裁量情节关系配置DAL
    /// </summary>
    public class Xzzf_WfClRelDAL
    {
        /// <summary>
        /// 插入地域违法行为与裁量情节关系配置表
        /// </summary>
        /// <param name="disWfClRels">地域违法行为与裁量情节关系models</param>
        /// <returns>返回执行是否成功 True成功 false失败</returns>
        public bool InsertWfClRel(List<Xzzf_WfClRel> disWfClRels)
        {
            try
            {
                return SqlManage.sqlServerJudgeDocSystem.Insert<Xzzf_WfClRel>(disWfClRels).ExecuteAffrows() > 0 ? true : false;
            }
            catch (Exception ex)
            {
                throw new Exception("插入地域违法行为与裁量情节关系配置表出错：" + ex.Message);
            }
        }

        /// <summary>
        /// 根据关系表主键id删除地域违法行为与裁量情节关系数据
        /// </summary>
        /// <param name="zjId">关系表主键id</param>
        /// <returns>返回执行是否成功 True成功 false失败</returns>
        public bool DelWfClRelByZjId(string zjId)
        {
            try
            {
                return SqlManage.sqlServerJudgeDocSystem.Delete<Xzzf_WfClRel>().Where(x => x.ZJ_ID == zjId).ExecuteAffrows() > 0 ? true : false;
            }
            catch (Exception ex)
            {
                throw new Exception("根据违法行为ccid删除地域与违法行为表关系数据出错：" + ex.Message);
            }
        }

        /// <summary>
        /// 获取地域违法行为与裁量情节关系配置
        /// </summary>
        /// <param name="ccId">裁量情节ccid</param>
        /// <returns>返回地域违法行为与裁量情节关系查询Model</returns>
        public List<Xzzf_WfClRelCXModel> GetDisWfRelData(string ccId)
        {
            try
            {
                List<Xzzf_WfClRelCXModel> models = SqlManage.sqlServerJudgeDocSystem.Select<Xzzf_DisWfRel, SYS_CaseCause, SYS_District, Xzzf_WfClRel>()
                    .InnerJoin((a, b, c, d) => a.WF_ID == b.CC_ID)
                    .InnerJoin((a, b, c, d) => a.CC_District == c.DT_ID)
                    .InnerJoin((a, b, c, d) => a.RC_ID == d.RC_ID)
                    .Where((a, b, c, d) => d.CL_ID == ccId)
                    .ToList((a, b, c, d) => new Xzzf_WfClRelCXModel { ZJ_ID = d.ZJ_ID, CC_District = a.CC_District, DistrictName = c.DT_Name, WF_ID = a.WF_ID, CC_Name = b.CC_Name });
                return models;
            }
            catch (Exception ex)
            {
                throw new Exception("获取地域违法行为与裁量情节关系配置出错：" + ex.Message);
            }
        }
    }
}
